var SignUtils = {

	parseSign (params, key) {

		params = Object.assign(params, {
			timestamp: (+new Date()).toString(),
			nonceStr : this.getNonceStr(),
		});

		params.sign = this.calcSign(params, key);

		return params;
	},

	calcSign (params, key) {

		var query = [];

		for (var k in params) {
			if (k !== 'sign') {
				if (params[k] !== '') {
					query.push(`${k}=${params[k]}`);
				}
			}
		}

		query.sort();
		query.push(`key=${key}`);
		query = query.join('&');

		var sign = this.md5(query).toUpperCase();

		return sign;
	},

	md5 (str) {
		// todo 调用md5算法
		console.warn('未实现MD5算法');
		return str;
	},

	// 随机字符串
	getNonceStr() {
	  var str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	  var pwd = ""
	  for (var i = 0; i < 16; i++) {
	    var index = Math.floor(Math.random() * str.length)
	    pwd += str.charAt(index)
	  }
	  return pwd
	},
}



var params = {
	appId: 'fbfc472044a3dd578d901f31',
	mchUrl: 'test.com',
};

var key = '4f5cadd0b03bd7df8ebc359d19fd1574eba829ce';

params = SignUtils.parseSign(params, key);

console.log(params);